﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Diagnostics;
using System.Device.Location;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using INdTFramework.Maps;
using Microsoft.Phone.Maps.Services;

namespace MapSample
{
    public partial class MapWithResearch : PhoneApplicationPage
    {
        // Object to assist in manipulating the map control.
        HelperMap helperMap;

        // Object that will receive the reference coordinates.
        GeoCoordinate coordinates;

        // List that will store the latest results of the address search.
        List<MapLocation> addressesFound;

        public MapWithResearch()
        {
            InitializeComponent();
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            // Creation of an instance of the class HelperMap with the map control that is inserted into the view.
            helperMap = new HelperMap(MyMap);

            // Reference coordinates for the address search.
            coordinates = new GeoCoordinate(-3.12702496536076, -60.0169777404517);

            // The reference coordinates become the center of the map control.
            MyMap.Center = coordinates;
            ShowReferenceMarker();
        }

        private void GoToMainPage_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }

        void ShowReferenceMarker()
        {
            // Image that represents the reference marker.
            var image = new Image { Source = new BitmapImage(new Uri("/Assets/pin1.png", UriKind.Relative)) };

            // Addition of the reference marker on the map control.
            helperMap.AddMarker(coordinates, image, new Point(0.5, 0.5));
        }

        private void Search_Click(object sender, EventArgs e)
        {
            ProgressIndicator.Visibility = Visibility.Visible;

            if (string.IsNullOrWhiteSpace(SearchTerm.Text))
            {
                ProgressIndicator.Visibility = Visibility.Collapsed;
                MessageBox.Show("Please enter a valid search data.", "Attention", MessageBoxButton.OK);
            }
            else
            {
                // If the search term is valid, the results of the previous research are removed with the function CleanAddresses.
                CleanAddresses();
                ProgressIndicator.Focus();

                // Request for address search from a search term and of the reference coordinates.
                // The ShowResult function is required to perform some action with the result of the address search.
                helperMap.SearchForTerm(SearchTerm.Text, coordinates, ShowResult);
            }
        }

        void ShowResult(List<MapLocation> list)
        {
            // Last search results are stored.
            addressesFound = list;

            // The research brings as a result a list of objects of type MapLocation, which contains information such as the coordinates, name of the continent, country, state, city and street.
            foreach (var mapLocation in list)
            {
                // Image that represents the marker.
                var image = new Image { Source = new BitmapImage(new Uri("/Assets/pin2.png", UriKind.Relative)) };

                // Addition of the marker on the map control.
                helperMap.AddMarker(mapLocation.GeoCoordinate, image, new Point(0.5, 1));
            }

            ProgressIndicator.Visibility = Visibility.Collapsed;
        }

        void CleanAddresses()
        {
            if (addressesFound != null)
            {
                foreach (var mapLocation in addressesFound)
                {
                    // Removal of a marker by its coordinates.
                    helperMap.RemoveMarker(mapLocation.GeoCoordinate);
                }
            }
        }
    }
}